﻿using Hangfire;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.EquipmentProcessDomain;
using Zocono.WCS.Domain.JobExcutorDomain;
using Zocono.WCS.Domain.WcsGetWmsJobDomain;

namespace Zocono.WCS.HangfireHost.TaskJobs
{
    /// <summary>
    /// 获取数据库数据存到缓存
    /// </summary>
    public class RepeatGetTableDataJob
    {
        private readonly IProcessPlanarService _processPlanarService;
        private readonly IConfiguration _configuration;
        private readonly IJobExcutorService _jobExcutorService;
        private readonly NlogUtilScoped _logUtil;
        private readonly TaskLock _taskLock;
        private static readonly object lockObj = new object();
        //private readonly Mutex mutex = new Mutex(false, "RepeatGetTableDataJob");

        public RepeatGetTableDataJob(IProcessPlanarService processPlanarService,
            IConfiguration configuration,
            IJobExcutorService jobExcutorService,
            NlogUtilScoped logUtil,
            TaskLock taskLock)
        {
            _processPlanarService = processPlanarService;
            _configuration = configuration;
            _jobExcutorService = jobExcutorService;
            _logUtil = logUtil;
            _taskLock = taskLock;
        }

        /// <summary>
        /// 执行作业任务
        /// </summary>
        /// <returns></returns>
        public async Task DoJob()
        {

            _logUtil.Debug($"开始读取监控数据...");
            if (_taskLock.DataToMenTaskLock)
            {
                _logUtil.Debug($"WCS数据库数据存内存任务正在执行中...");
                return;
            }
            try
            {

                _taskLock.DataToMenTaskLock = true;
                _logUtil.Debug("获取PLC设备监控数据Job0，开始");
                //获取监控配置Job
                var equipmentStates = _configuration.GetSection("EquipmentState").Get<List<EquipmentState>>();
                _logUtil.Debug("获取PLC设备监控数据Job1");
                //执行读取监控数据定时任务
                foreach (var item in equipmentStates)
                {
                    await _jobExcutorService.ReadPlcDbMonitorAsync(item.ProgrameName, item.Jobs);
                    _logUtil.Debug("获取PLC设备监控数据Job2");
                }
                //读取业务配置数据到redis
                await _processPlanarService.ReloadPlanarActionMessage();
            }
            catch (Exception ex)
            {
                _logUtil.Error($"WCS数据库数据存内存任务报错ex={ex}");
            }
            finally
            {
                _taskLock.DataToMenTaskLock = false;
                _logUtil.Debug("获取PLC设备监控数据Job3，完成");
            }

        }

        /// <summary>
        /// 使用hangfire时调用
        /// </summary>
        /// <returns></returns>
        public async Task HangfireJob()
        {
            await DoJob();
        }
    }
}
